import java.util.Arrays;
import java.util.Scanner;
public class NGuessNumber {
    public static int q_num = 0;
    public static int w_num = 0;
    public static int[] guesslist=new int[4];
    public static int[] numbers=new int[4];
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("游戏开始:");
            generateAnswer();
            while (true) {
                getPlayerInput();
                compareGuessAnswer();
                if (isWin()) {
                    System.out.println("恭喜你游戏胜利啦");
                    break;
                } else {
                    System.out.println("猜错了，再试一次吧");
                }
            }
            System.out.println("在玩一次(y/n)");
            String choice = scanner.next();
            if (!choice.equalsIgnoreCase("y")) {
                break;
            }
        }
    }
    public static String compareGuessAnswer(){
        q_num=0;
        w_num=0;
        for (int i=0;i<numbers.length;i++){
            for (int j=0;j<numbers.length;j++){
                if (i!=j){
                    if (numbers[i]==guesslist[j]){
                        w_num++;
                    }
                }else {
                    if (numbers[i]==guesslist[j]){
                        q_num++;
                    }
                }
            }
        }
        System.out.println(q_num+"A"+w_num+"B");
        System.out.println("A表示存在且位置正确的数字个数，B表示存在且位置不正确的数字个数！");
        System.out.println("如：1A2B,表示你猜测的四个数字有3个是对的，有一个位置对，两个位置不对。\n");
        return q_num+"A"+w_num+"B\n"+"A表示存在且位置正确的数字个数，B表示存在且位置不正确的数字个数！\n"+"如：1A2B,表示你猜测的四个数字有3个是对的，有一个位置对，两个位置不对。";
    }
    public static int[] getPlayerInput(){
        try {
            Scanner sca = new Scanner(System.in);
            for (int i = 0; i < 4; i++) {
                System.out.println("请输入0-9之间的第" + (i+1) + "个数字：");
                int pl = sca.nextInt();
                if (pl >= 10 || pl < 0) {
                    System.out.print("输入错误，");
                    i=i-1;
                }else {
                    guesslist[i] = pl;
                }
            }
        }catch (Exception e){
            System.out.println("输入错误");
        }
        System.out.println(Arrays.toString(guesslist)+"\n");
        return guesslist;
    }
    public static int[] generateAnswer(){
        int x = 0;
        while (true) {
            int random = (int)(Math.random() * 9 + 1);
            int i = 0;
            for ( ; i < numbers.length; i++) {
                if(random == numbers[i]) {
                    break;
                }
            }
            if(i == numbers.length ) {
                numbers[x++] = random;
            }
            if (x == 4) {
                break;
            }
        }
        return numbers;
    }
    public static boolean isWin(){
        boolean flag = false;
        if(q_num==4){
            flag=true;
        }else {
            flag=false;
        }
        return flag;
    }
}
